package cn.itsource.order.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.LoginContextUtils;
import cn.itsource.basic.utils.PageResult;
import cn.itsource.order.domain.OrderPetAcquisition;
import cn.itsource.order.dto.OrderDto;
import cn.itsource.order.mapper.OrderPetAcquisitionMapper;
import cn.itsource.order.query.OrderPetAcquisitionQuery;
import cn.itsource.order.service.IOrderPetAcquisitionService;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;

    @Override
    public PageResult<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContextUtils.getLogininfo(request);
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        // 判断登录员工是那种角色
        //1.employee中shop_id为null，平台管理员
        Long shopId = employee.getShopId();
        //2.店铺人员
        if (shopId!=null){
            Shop shop = shopMapper.findOne(shopId);
            if(!shop.getAdminId().equals(employee.getId())){//店员 //注意：Long包装类不能用==判断，long才行
                //3.否则就为店员
                query.setEmpId(employee.getId());
            }
            //4.employee中shop_id不为null，通过shop_id查询店铺得到admin_id如果为当前登录管理员的id，就为店铺管理员
            query.setShopId(shopId);
        }
        //5.平台管理员
        return super.queryPage(query);
    }

    @Override
    @Transactional
    public AjaxResult cancelOrder(Long id) {
        //将订单状态改为-1
        OrderPetAcquisition order = orderPetAcquisitionMapper.findOne(id);
        order.setState(-1);//宠物没有接成功，订单取消
        orderPetAcquisitionMapper.update(order);

        //将寻主消息放入寻主池：state=3，shopId=null
        SearchMasterMsg msg = searchMasterMsgMapper.findOne(order.getSearchMasterMsgId());
        searchMasterMsgMapper.reject(msg.getId());

        return new AjaxResult();
    }

    //确认订单
    @Override
    public void confirm(OrderDto orderDto) {
        // orderId ，支付方式，成交价
        //1.生成宠物信息
        OrderPetAcquisition order = orderPetAcquisitionMapper.findOne(orderDto.getId());
        SearchMasterMsg msg = searchMasterMsgMapper.findOne(order.getSearchMasterMsgId());
        Pet pet = msg2Pet(msg,orderDto.getMoney());
        petMapper.add(pet);

        //2.改订单状态为1，价格为成交价，支付方式
        order.setState(1);
        order.setPaytype(orderDto.getPayType());
        order.setPrice(orderDto.getMoney());
        order.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(order);

        //3.改寻主消息状态2
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
    }
    private Pet msg2Pet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(money);
        pet.setCreatetime(new Date());
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
        pet.setUserId(msg.getUserId());
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }
}
